package com.frxuexi.mdm2203.view

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.frxuexi.lib_base.RouterPath
import com.frxuexi.mdm2203.R

data class HomeEntity(val title:String,val img:Int)

@Composable
fun HomePage (navHostController: NavHostController){

    val list = arrayOf(
        HomeEntity("在线应用", R.drawable.icon32),
//        HomeEntity("本地应用", R.drawable.icon32),
        HomeEntity("考勤打卡", R.drawable.icon1912),
//        HomeEntity("智能审批", R.drawable.icon81),
        HomeEntity("工作日志", R.drawable.icon359),
//        HomeEntity("任务", R.drawable.icon105),
        HomeEntity("文件", R.drawable.icon3)
    )

    val list2 = arrayOf(
        HomeEntity("联系人", R.drawable.icon1887),
        HomeEntity("讨论组", R.drawable.icon980),
        HomeEntity("组织架构", R.drawable.icon1907)
    )

    Column {
        Row(
            Modifier
                .height(60.dp)
                .fillMaxWidth()
                .background(colorResource(id = R.color.lightblue))
                .padding(vertical = 10.dp, horizontal = 20.dp), verticalAlignment = Alignment.CenterVertically) {
            Text(text = "12", color = Color.White, modifier = Modifier
                .clip(CircleShape)
                .background(colorResource(id = R.color.pink))
                .padding(4.dp))
            Spacer(modifier = Modifier.width(10.dp))
            Text(text = "主门户")
        }
        TitltItem(homeEntity = HomeEntity("应用",R.drawable.icon1382))
        LazyVerticalGrid(verticalArrangement= Arrangement.spacedBy(10.dp),columns = GridCells.Fixed(3), content = {
            items(list){
                ContentItem(homeEntity = it,navHostController)
            }
        })
        TitltItem(homeEntity = HomeEntity("通信",R.drawable.icon1488))
        LazyVerticalGrid(verticalArrangement= Arrangement.spacedBy(10.dp),columns = GridCells.Fixed(3), content = {
            items(list2){
                ContentItem(homeEntity = it,navHostController)
            }
        })
        TitltItem(homeEntity = HomeEntity("公众号",R.drawable.icon1488))
    }
}


@Composable
fun TitltItem(homeEntity: HomeEntity){
    Row(Modifier.padding(vertical = 10.dp, horizontal = 20.dp),
        verticalAlignment = Alignment.CenterVertically) {
        Image(painter = painterResource(id = homeEntity.img), contentDescription = "",
            modifier = Modifier.size(30.dp))
        Spacer(modifier = Modifier.width(10.dp))
        Text(text = homeEntity.title)
    }
}

@Composable
fun ContentItem(homeEntity: HomeEntity,navHostController: NavHostController){
    Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.clickable { NavToPage(homeEntity.title,navHostController) }) {
        Image(painter = painterResource(id = homeEntity.img), contentDescription = "",
            modifier = Modifier.size(50.dp))
        Spacer(modifier = Modifier.height(5.dp))
        Text(text = homeEntity.title)
    }
}

fun NavToPage(title: String,navHostController: NavHostController){
    if(title=="在线应用"){
        navHostController.navigate(RouterPath.APPS_PAGE)
    }else if(title=="文件"){
        navHostController.navigate(RouterPath.DOC_PAGE)
    }else if(title=="工作日志"){
        navHostController.navigate(RouterPath.REPORT_PAGE)
    }else if(title=="考勤打卡"){
        navHostController.navigate(RouterPath.SIGN_PAGE)
    }
    else if(title=="任务"){
        navHostController.navigate(RouterPath.TASK_PAGE)
    }
    else if(title=="智能审批"){
        navHostController.navigate(RouterPath.TASKLIST_PAGE)
    }
}